<?php
$INCLUDE_DIR = DOCUROOT."/include/class/phpmailer/";
require_once($INCLUDE_DIR . "class.phpmailer.php");
error_reporting(0);
/**
##	File:	class_mailer.php
##	By kokko<kokko313@gmail.com>
##	@Version 1.0<2006-08-13>
##	邮件类
 */
class Mailer 
{
    var $Mail = false;
    var $Host = "";
	var $Username = "";	
	var $Password = "";
	var $ErrorInfo=null;
	
    /**
     * Class constuctor.
     */
    function Mailer() {
        global $global_vars;
        global $INCLUDE_DIR;

        $this->Mail = new PHPMailer();

        $this->Mail->Priority = 3;
        $this->Mail->Encoding = "base64";
        $this->Mail->CharSet = "iso-8859-1";
        $this->Mail->From = "unit_test@phpmailer.sf.net";
        $this->Mail->FromName = "Unit Tester";
        $this->Mail->Sender = "";
        $this->Mail->Subject = "";
        $this->Mail->Body = "";
        $this->Mail->AltBody = "";
        $this->Mail->WordWrap = 0;
        $this->Mail->Port = 25;
        $this->Mail->Helo = "eefocus.com";
        $this->Mail->SMTPAuth = true;
        $this->Mail->Host = "localhost";
        $this->Mail->Username = "webmaster@eefocus.com";
        $this->Mail->Password = "#*sendmail*#";
        $this->Mail->PluginDir = $INCLUDE_DIR;
//		$this->Mail->AddReplyTo("no_reply@phpmailer.sf.net", "Reply Guy");
		$this->Mail->SMTPDebug=false;

        if(strlen($this->Mail->Host) > 0)
            $this->Mail->Mailer = "smtp";
        else
        {
            $this->Mail->Mailer = "mail";
        }
        
    }
	
	/**
	 * 发送Email邮件
	 *
	 * @static
	 * @param string $toMail
	 * @param string $fromMail
	 * @param string $mailSubject
	 * @param string $mailBody
	 * @param string $fromName
	 * @param string $toName
	 * @param string $charset
	 * @param int $priority
	 * @return mix
	 */
	public static function sentEmail( $toMail,$fromMail,$mailSubject,$mailBody,$fromName='',$toName='',$charset='UTF-8',$priority=3 ){
		static $mailer = null;
		$smtpHost = 'mail.eefocus.com';
		$smtpUsername = 'admin@eefocus.com';
		$smtpPassword = 'admin123';
		if( $mailer==null ){
			$mailer = new Mailer();
			$mailer->Mail->SMTPDebug = false;
			$mailer->SetSMTPAuth( $smtpHost,$smtpUsername,$smtpPassword );
		}
		set_time_limit(120);
		$mailer->SetPriority($priority);
		$mailer->SetAddress($toMail,$toName);
		$mailer->SetFrom($fromMail,$fromName);
		$mailer->SetCharSet($charset);
		$mailer->SetSubject($mailSubject);
		$mailer->SetBody($mailBody);
		$errorInfo = $mailer->ErrorInfo;
		return $mailer->test_Html();
	}

    /**
     * Build the body of the message in the appropriate format.
     * @private
     * @returns void
     */
    function BuildBody() {
        
        // Determine line endings for message        
        if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)
        {
            $eol = "<br/>";
            $bullet = "<li>";
            $bullet_start = "<ul>";
            $bullet_end = "</ul>";
        }
        else
        {
            $eol = "\n";
            $bullet = " - ";
            $bullet_start = "";
            $bullet_end = "";
        }
        
        $ReportBody = "";
        
        $ReportBody .= "---------------------" . $eol;
        $ReportBody .= "www.eefocus.com Message. don't reply." . $eol;
        $ReportBody .= "---------------------" . $eol;
        
        $ReportBody .= $bullet_start;
        $ReportBody .= $bullet_end . $eol;
       
        
        // Re-attach the original body
//        $this->Mail->Body .= $eol . $eol . $ReportBody;
    }
    
    
    function SetSender($sSender, $sName = "") {
        if($sName!='')
        {
                $this->Mail->Sender="$sName <$sAddress>";
        }
		else{
			$this->Mail->Sender=$sAddress;
		}
    }

    function SetFrom($sSender, $sName = "") {
        if($sName!='')
        {
                $this->Mail->From=$sSender;
				$this->Mail->FromName=$sName;
        }
		else{
			$this->Mail->From=$this->Mail->Sender;
			$this->Mail->FromName='';
		}
    }

    function SetEncoding($sEncoding) {
        if($sEncoding!='')
        {
                $this->Mail->Encoding=$sEncoding;
        }
    }

    function SetCharSet($sCharSet='GBK') {
                $this->Mail->CharSet=$sCharSet;
    }

    function SetSubject($strval='Invitation') {
                $this->Mail->Subject=$strval;
    }

    function SetPriority($strval) {
		if($strval=='')
			return ;
        $this->Mail->Priority = intval($strval);
    }

    function SetBody($strval) {
		if($strval=='')
			return ;
        $this->Mail->Body=$strval;
    }

    function SetSMTPAuth($Host,$Username,$Password) {
		$this->Mail->Host=$Host;
		$this->Mail->Username=$Username;
		$this->Mail->Password=$Password;
        if(strlen($this->Mail->Host) > 0)
            $this->Mail->Mailer = "smtp";
        else
        {
            $this->Mail->Mailer = "mail";
        }
    }

	/**
     * Adds all of the addresses
     * @public
     * @returns void
     */
    function SetAddress($sAddress, $sName = "", $sType = "to") {
        switch($sType)
        {
            case "to":
                $this->Mail->AddAddress($sAddress, $sName);
                break;
            case "cc":
                $this->Mail->AddCC($sAddress, $sName);
                break;
            case "bcc":
                $this->Mail->AddBCC($sAddress, $sName);
                break;
        }
    }

    /////////////////////////////////////////////////
    // UNIT TESTS
    /////////////////////////////////////////////////

    /**
     * Try a plain message.
     */
    function test_WordWrap() {

        $this->Mail->WordWrap = 40;
        $my_body = "Here is the main body of this message.  It should " .
                   "be quite a few lines.  It should be wrapped at the " .
                   "40 characters.  Make sure that it is.";
        $nBodyLen = strlen($my_body);
        $my_body .= "\n\nThis is the above body length: " . $nBodyLen;

        $this->Mail->Body = $my_body;
        $this->Mail->Subject .= ": Wordwrap";

        $this->BuildBody();
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
    }

    /**
     * Try a plain message.
     */
    function test_Low_Priority() {
    
        $this->Mail->Priority = 5;
        $this->Mail->Body = "Here is the main body.  There should be " .
                            "a reply to address in this message.";
        $this->Mail->Subject .= ": Low Priority";
        $this->Mail->AddReplyTo("nobody@nobody.com", "Nobody (Unit Test)");

        $this->BuildBody();
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
    }

    /**
     * Simple plain file attachment test.
     */
    function test_Multiple_Plain_FileAttachment() {

        $this->Mail->Body = "Here is the text body";
        $this->Mail->Subject .= ": Plain + Multiple FileAttachments";

        if(!$this->Mail->AddAttachment("rocks.png"))
        {
            $this->assert(false, $this->Mail->ErrorInfo);
            return;
        }

        if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))
        {
            $this->assert(false, $this->Mail->ErrorInfo);
            return;
        }

        $this->BuildBody();
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
    }

    /**
     * Simple plain string attachment test.
     */
    function test_Plain_StringAttachment() {

        $this->Mail->Body = "Here is the text body";
        $this->Mail->Subject .= ": Plain + StringAttachment";
        
        $sAttachment = "These characters are the content of the " .
                       "string attachment.\nThis might be taken from a ".
                       "database or some other such thing. ";
        
        $this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");

        $this->BuildBody();
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
    }

    /**
     * Plain quoted-printable message.
     */
    function test_Quoted_Printable() {

        $this->Mail->Body = "Here is the main body";
        $this->Mail->Subject .= ": Plain + Quoted-printable";
        $this->Mail->Encoding = "quoted-printable";

        $this->BuildBody();
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
    }

    /**
     * Try a plain message.
     */
    function test_Html() {
		$this->ErrorInfo='';
    
        $this->Mail->IsHTML(true);
 /*       
        $this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .
                            "Go to <a href=\"http://phpmailer.sourceforge.net/\">" .
                            "http://phpmailer.sourceforge.net/</a> for new versions of " .
                            "phpmailer.  <p/> Thank you!";
*/
        $this->BuildBody();
        $ret=$this->Mail->Send();
		$this->ErrorInfo=$this->Mail->ErrorInfo;
		return $ret;
    }

    /**
     * Simple HTML and attachment test
     */
    function test_HTML_Attachment() {

        $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
        $this->Mail->Subject .= ": HTML + Attachment";
        $this->Mail->IsHTML(true);
        
        if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
        {
            $this->assert(false, $this->Mail->ErrorInfo);
            return;
        }

        $this->BuildBody();
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
    }

    /**
     * An embedded attachment test.
     */
    function test_Embedded_Image($path,$name,$mimetype= "application/octet-stream") {

//        $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .                     "Here is an image!</a>";
        $this->Mail->IsHTML(true);
        
        if(!$this->Mail->AddEmbeddedImage($path, "my-attach", $name,
                                          "base64", $mimetype))
        {
			$this->ErrorInfo=$this->Mail->ErrorInfo;
            return false;
        }

        $this->BuildBody();
        $ret=$this->Mail->Send();
		$this->ErrorInfo=$this->Mail->ErrorInfo;
		return $ret;
    }

    /**
     * An embedded attachment test.
     */
    function test_Multi_Embedded_Image($imgarray) {

        $this->Mail->IsHTML(true);
        

		foreach($imgarray as $val){
			if(!$this->Mail->AddEmbeddedImage($val['path'], $val['id'], $val['name'],
											  "base64", $val['type']))
			{
				$this->ErrorInfo=$this->Mail->ErrorInfo;
				return false;
			}
		}

        $this->BuildBody();
        $ret=$this->Mail->Send();
		$this->ErrorInfo=$this->Mail->ErrorInfo;
		return $ret;
    }

    /**
     * Simple multipart/alternative test.
     */
    function test_AltBody() {

        $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
        $this->Mail->AltBody = "Here is the text body of this message.  " .
                   "It should be quite a few lines.  It should be wrapped at the " .
                   "40 characters.  Make sure that it is.";
        $this->Mail->WordWrap = 40;
        $this->AddNote("This is a mulipart alternative email");
        $this->Mail->Subject .= ": AltBody + Word Wrap";

        $this->BuildBody();
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
    }

    /**
     * Simple HTML and attachment test
     */
    function test_AltBody_Attachment() {

        $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
        $this->Mail->AltBody = "This is the text part of the email.";
        $this->Mail->Subject .= ": AltBody + Attachment";
        $this->Mail->IsHTML(true);
        
        if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))
        {
            $this->assert(false, $this->Mail->ErrorInfo);
            return;
        }

        $this->BuildBody();
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);

        $fp = fopen("message.txt", "w");
        fwrite($fp, $this->Mail->CreateHeader() . $this->Mail->CreateBody());
        fclose($fp);
    }    

    function test_MultipleSend() {
        $this->Mail->Body = "Sending two messages without keepalive";
        $this->BuildBody();
        $subject = $this->Mail->Subject;

        $this->Mail->Subject = $subject . ": SMTP 1";
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
        
        $this->Mail->Subject = $subject . ": SMTP 2";
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
    }

    function test_SmtpKeepAlive() {
        $this->Mail->Body = "This was done using the SMTP keep-alive.";
        $this->BuildBody();
        $subject = $this->Mail->Subject;

        $this->Mail->SMTPKeepAlive = true;
        $this->Mail->Subject = $subject . ": SMTP keep-alive 1";
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
        
        $this->Mail->Subject = $subject . ": SMTP keep-alive 2";
        $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);
        $this->Mail->SmtpClose();
    }
    
    function test_Error() {
        $this->Mail->Subject .= ": This should be sent"; 
        $this->BuildBody();
        $this->Mail->ClearAllRecipients(); // no addresses should cause an error
        $this->assert($this->Mail->IsError() == false, "Error found");
        $this->assert($this->Mail->Send() == false, "Send succeeded");
        $this->assert($this->Mail->IsError(), "No error found");
        $this->assertEquals('You must provide at least one ' .
                            'recipient email address.', $this->Mail->ErrorInfo);
        $this->Mail->AddAddress(get("mail_to"));
        $this->assert($this->Mail->Send(), "Send failed");
    }
}  
?>